package com.ygou.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ygou.mapper.AdmininfoMapper;
import com.ygou.pojo.Admininfo;
import com.ygou.pojo.AdmininfoExample;
import com.ygou.pojo.AdmininfoExample.Criteria;
import com.ygou.service.AdminInfoService;
import com.ygou.utils.Tools;
@Service
public class AdminInfoServiceImpl implements AdminInfoService {
	@Autowired
	AdmininfoMapper admininfoMapper;
	
	@Override
	public Map<String, Object> showPage(Map<String, Object> m) {
		Object curr = m.get("currPage");
		int currPage = 1;
		if(curr!=null&&!curr.equals("")){
			currPage=Integer.parseInt(curr.toString());
		}
		AdmininfoExample e=new AdmininfoExample();
		PageHelper.startPage(currPage, 2);
		List<Admininfo> ls1=admininfoMapper.selectByExample(e);
		PageInfo<Admininfo> pages=new PageInfo<Admininfo>(ls1,2);
		m.put("pages",pages);
		return m;
	}

	@Override
	public boolean insert(Admininfo admininfo) {
		admininfo.setAdminstatus((byte)1);
		return admininfoMapper.insert(admininfo)>0;
	} 

	@Override
	public Admininfo selectByPrimaryKey(Integer adminid) {
		return admininfoMapper.selectByPrimaryKey(adminid);
	}

	@Override
	public boolean updateByPrimaryKey(Admininfo admininfo) {
		return admininfoMapper.updateByPrimaryKey(admininfo)>0;
	}

	@Override
	public List<Admininfo> findByWhere(String loginname,String regtime) {
		AdmininfoExample example = new AdmininfoExample();
		Criteria c = example.createCriteria();
		if(!"".equals(loginname)&&loginname!=null){
			c.andLoginnameLike("%"+loginname+"%");
		}
		if(regtime!=null &&!"".equals(regtime)){
			c.andRegtimeLike("%"+regtime+"%");
		}
		return admininfoMapper.selectByExample(example);
	}

	@Override
	public Admininfo login(String loginname, String password,HttpServletRequest request) {
		AdmininfoExample  example = new AdmininfoExample();
		Criteria c = example.createCriteria();
		if(!"".equals(loginname)&&loginname!=null){
			c.andLoginnameEqualTo(loginname);
		}
		if(password !=null &&!"".equals(password)){
			c.andLoginpwdEqualTo(password);
		}
		
		if(admininfoMapper.selectByExample(example).size()!=0){
			Admininfo admin=admininfoMapper.selectByExample(example).get(0);
			//登录成功后  把最后登录的时间设置为当前时间
			//登录成功后 吧最后登录的ip地址换成当前的ip地址
			admin.setLasttime(new java.util.Date());
			admin.setLastip(Tools.getLocalIp(request));
			admininfoMapper.updateByPrimaryKeySelective(admin);
			return admin;
		}
		return null;
	}

	@Override
	public boolean updateByPrimaryKeySelective(Admininfo admininfo) {
		return admininfoMapper.updateByPrimaryKeySelective(admininfo)>0;
	}

}
